# -*- coding: utf-8 -*-

import logger.logger as logger
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression



class LR():
    def __init__(self, penalty='l2',
                 dual=False,
                 tol=1e-4,
                 C=1.0,
                 fit_intercept=True,
                 intercept_scaling=1,
                 class_weight=None,
                 random_state=None,
                 solver='liblinear',
                 max_iter=100,
                 multi_class='ovr',
                 verbose=0,
                 warm_start=False,
                 n_jobs=1):
        self.model = None
        self.parameter = {}
        self.parameter['penalty'] = penalty
        self.parameter['dual'] = dual
        self.parameter['tol'] = tol
        self.parameter['C'] = C
        self.parameter['fit_intercept'] = fit_intercept
        self.parameter['intercept_scaling'] = intercept_scaling
        self.parameter['class_weight'] = class_weight
        self.parameter['random_state'] = random_state
        self.parameter['solver'] = solver
        self.parameter['max_iter'] = max_iter
        self.parameter['multi_class'] = multi_class
        self.parameter['class_weight'] = class_weight
        self.parameter['verbose'] = verbose
        self.parameter['warm_start'] = warm_start
        self.parameter['n_jobs'] = n_jobs

    def train(self, x_train, y_train, x_test, y_test, model_path):
        logger.info("training...")
        clf = LogisticRegression(**self.parameter)
        clf.fit(x_train, y_train)
        joblib.dump(clf, model_path)
        acc = clf.score(x_test, y_test)
        logger.info("acc: {}".format(str(acc)))

    def cross_validation(self, train_x, train_y, test_vecs, y_test, model_path, param_grid=None):
        from sklearn.model_selection import GridSearchCV
        model = LogisticRegression()
        param_grid = [
            {'kernel': ['rbf'],
             'C': [0.001, 0.01, 0.1, 1, 10, 100],
             'gamma': [0.001, 0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6]},

            {'kernel': ['linear'],
             'C': [0.001, 0.01, 0.1, 1, 10, 10]}
        ]
        grid_search = GridSearchCV(model, param_grid, n_jobs=4, verbose=1, cv=5)
        grid_search.fit(train_x, train_y)
        best_parameters = grid_search.best_estimator_.get_params()
        for para, val in list(best_parameters.items()):
            print(para, val)

        self.model = LogisticRegression(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True)
        self.model.fit(train_x, train_y)
        joblib.dump(self.model, model_path)
        acc = self.model.score(test_vecs, y_test)
        logger.info("acc: {}".format(str(acc)))

    def load_model(self, model_path):
        self.model = joblib.load(model_path)

    def predict(self, x_vecs):
        if not self.model:
            logger.info("please load logistic regression model first!!!")
            return
        y_pred = self.model.predict(x_vecs)
        return y_pred



if __name__ == '__main__':
    pass